/**
 * Created by dukes on 2015/12/4.
 */
class ChatPopPanel extends eui.Component {
    private data;
    public constructor() {
        super();
        this.skinName = "pet.ChatPopSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addedToStage, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removedFromStage, this);
    }
    private onBlack(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        window["__block"](this.data.userId);
        this.hide();
    }
    private onVisit(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        SceneMgr.getInstance().switchScene(SceneMgr.SCENE_GETOUT,this.data);
        this.hide();
    }
    private onChat(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        ChatMgr.getInstance().addChatList(this.data);
        SceneMgr.getInstance().switchScene(SceneMgr.SCENE_CHAT_P,[this.data]);
        this.hide();
    }
    public show(data:any,stagex:number,stagey:number){
        this.data = data;
        var rx = stagex - 110;
        rx = rx > 0 ? rx : 0;
        this.x = rx;
        this.y = stagey+10;
        DisplayController.instance().addToAlert(this);
    }
    public hide(evt:egret.TouchEvent=null){
        if( this.parent)
            DisplayController.instance().removeFromAlert(this);
    }
    private img_chat:eui.TextInput;
    private img_visit:eui.Image;
    private img_blacklist:eui.TabBar;
    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_chat") {
            this.img_chat = instance;
        } else if (name == "img_visit") {
            this.img_visit = instance;
        } else if (name == "img_blacklist") {
            this.img_blacklist = instance;
        }
    }
    private addedToStage(evt:egret.Event){
        this.img_chat.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChat, this);
        this.img_visit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onVisit, this);
        this.img_blacklist.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBlack, this);
        setTimeout(function(callObj){
            GameConst.Stage.addEventListener(egret.TouchEvent.TOUCH_TAP, callObj.hide, callObj);
        },500,this);
    }

    private removedFromStage(evt:egret.Event){
        this.img_chat.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChat, this);
        this.img_visit.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onVisit, this);
        this.img_blacklist.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onBlack, this);
        GameConst.Stage.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.hide, this);
    }

    private static _instance:ChatPopPanel;
    public static getInstance():ChatPopPanel{
        if( ChatPopPanel._instance == null)
            ChatPopPanel._instance = new ChatPopPanel();
        return ChatPopPanel._instance;
    }
}